<?php
namespace Admin\Controller;
use Admin\Controller;
/**
 * 轮播图管理
 */
class BannerController extends BaseController
{
    /**
     * 列表
     * @return [type] [description]
     */
    public function index($key="")
    {
        $banners    = M('banners'); // 实例化User对象
        $where      = "status >= 0";
        $count      = $banners->where($where)->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $this->page = $Page->show();// 分页显示输出
        $this->list = $banners->where($where)->order(array('status' => 'desc', 'id' => 'desc'))->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->display(); // 输出模板
    }

    /**
     * 添加
     */
    public function add()
    {
        //默认显示添加表单
        if (IS_POST) {
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =      C('BANNER_PATH'); // 设置附件上传根目录
            // 取得成功上传的文件信息
            $info = $upload->upload();
            if(!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else{// 上传成功 获取上传文件信息
                $data['image'] = $info['file']['savepath'].$info['file']['savename'];
            }
            // 保存当前数据对象
            $data['create_time'] = NOW_TIME;
            $data['status'] = I('post.status');
            //$data['titles']  = I('post.title');

            if (M('banners')->add($data)) {
                $this->success("添加成功", U('banner/index'));
            } else {
                $this->error("添加失败");
            }

        }else{
            $this->display();
        }
    }
    /**
     * 更新
     * @param  [type] $id [分类ID]
     * @return [type]     [description]
     */
    public function update()
    {
        //默认显示添加表单
        if (IS_POST) {
            $id = I('post.banner_id');
            //判断是否上传图片
            if($_FILES['file']['name']){
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     3145728 ;// 设置附件上传大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath  =      C('BANNER_PATH'); // 设置附件上传根目录
                // 取得成功上传的文件信息
                $info = $upload->upload();
                if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{// 上传成功 获取上传文件信息
                    $data['image'] = $info['file']['savepath'].$info['file']['savename'];
                }
                // 保存当前数据对象
                //$data['titles']  = I('post.title');
                $data['update_time'] = NOW_TIME;
                $data['status'] = I('post.status');

                if (M('banners')->where(array('id' => $id))->save($data)) {
                    $this->success("编辑成功", U('banner/index'));
                } else {
                    $this->error("编辑失败");
                }

            }else{

                $data['titles']  = I('post.title');
                $data['update_time'] = NOW_TIME;
                $data['status'] = I('post.status');

                if (M('banners')->where(array('id' => $id))->save($data)) {
                    $this->success("编辑成功", U('banner/index'));
                } else {
                    $this->error("编辑失败");
                }
            }
        }else{
            $this->info = M('banners')->find(I('id'));

            $this->display();
        }
    }
    /**
     * 删除分类
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete($id)
    {
        $data['status'] = -1;
        if (M('banners')->where(array('id' => $id))->save($data)) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }
}
